package com.paipaideli.ui.mine.head;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.PullRefreshView;
import com.paipaideli.common.pullrefresh.TStatusView;
import com.paipaideli.common.utils.DateUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.mine.head.bean.ProfitBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 累计收益
 */
public class ProfitActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.tv_profit_total)
    TextView tv_profit_total;
    @BindView(R.id.profit_recycleview)
    PullRefreshView profit_recycleview;
    @BindView(R.id.bg_statusview)
    TStatusView bg_statusview;

    private ProfitAdapter profitAdapter;

    private List<ProfitBean.Data.RewardRes> list = new ArrayList<>();
    private int curPage = 1;
    private int total;
    private int pageSize;

    @Override
    public int getContentView() {
        return R.layout.activity_profit;
    }

    @Override
    public void initView() {

        top_title.setText("累计收益");
        top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        profit_recycleview.setEnableRefresh(true);
        //设置是否在全部加载结束之后Footer跟随内容
        profit_recycleview.setOnRefreshListener(refreshlayout -> {
            list.clear();
            curPage = 1;
            bg_statusview.showLoading();
            initDate();
            profit_recycleview.finishRefresh();
        });

        profit_recycleview.setLoadmoreListener(refreshlayout -> {
            double p = Math.ceil(Double.valueOf(total)/Double.valueOf(pageSize));
            int page = Integer.valueOf((int) p);
            if (curPage < page) {
                curPage++;
                initDate();
            } else {
                //加载完成 并且 标记为没有更多数据
                refreshlayout.finishLoadmoreWithNoMoreData();
            }
            profit_recycleview.finishLoadmore();
        });
        profitAdapter = new ProfitAdapter();
        profit_recycleview.setLayoutManager(new LinearLayoutManager(this));
        profit_recycleview.setAdapter(profitAdapter);

        bg_statusview.showLoading();
        initDate();
    }


    public void initDate() {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("curPage", curPage);
            requestData.put("pageSize", "20");
        } catch (Exception e) {
            e.getMessage();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().record(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    if (request.code.equals("200")) {
                        list.addAll(request.data.rewardRes);
                        total = request.total;
                        curPage = request.curPage;
                        pageSize = request.pageSize;
                        tv_profit_total.setText(request.data.bounty);
                        profitAdapter.notifyDataSetChanged();
                        bg_statusview.finish();
                        ToastUtil.show(request.msg);
                    } else {
                        bg_statusview.finish();
                        ToastUtil.show(request.msg);
                    }
                }, throwable -> {
                    bg_statusview.finish();
                    Log.e(TAG, "error", throwable);
                });
    }

    public class ProfitAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(ProfitActivity.this).inflate(R.layout.item_profit_view, null);
            inflate.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
            if (list.get(position).rewardType.equals("0")){
                viewHolder.text_price_name.setText("初级场收益");
            }else if (list.get(position).rewardType.equals("1")){
                viewHolder.text_price_name.setText("正式场收益");
            }else if (list.get(position).rewardType.equals("2")){
                viewHolder.text_price_name.setText("推荐用户收益");
            }else if (list.get(position).rewardType.equals("3")){
                viewHolder.text_price_name.setText("团对收益");
            }else {
                viewHolder.text_price_name.setText("终极场收益");
            }
            viewHolder.text_price_time.setText(DateUtil.getDateToString(list.get(position).createTime));
            viewHolder.text_price_money.setText(list.get(position).acquirableMoney);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.lin_profit)
            LinearLayout lin_profit;
            @BindView(R.id.text_price_name)
            TextView text_price_name;
            @BindView(R.id.text_price_time)
            TextView text_price_time;
            @BindView(R.id.text_price_money)
            TextView text_price_money;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
